package com.liujit.validator.domain;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: liujun
 * @date: 2021/6/18 1:17 下午
 * @description: 验证结果类
 */
@Data
public class ValidResult {

    /**
     * 是否有错误
     */
    private boolean hasErrors;

    /**
     * 错误信息
     */
    private List<ErrorMessage> errors;

    public ValidResult() {
        this.errors = new ArrayList<>();
    }

    public ValidResult(List<ErrorMessage> errors) {
        this.errors = errors;
    }

    public boolean hasErrors() {
        return hasErrors;
    }

    public void setHasErrors(boolean hasErrors) {
        this.hasErrors = hasErrors;
    }

    /**
     * 获取所有验证信息
     *
     * @return 集合形式
     * @author: liujun
     * @date: 2021/6/18 1:16 下午
     */
    public List<ErrorMessage> getAllErrors() {
        return errors;
    }

    /**
     * 获取所有验证信息
     *
     * @return 字符串形式
     * @author: liujun
     * @date: 2021/6/18 1:16 下午
     */
    public String getErrors() {
        StringBuilder sb = new StringBuilder();
        for (ErrorMessage error : errors) {
            sb.append(error.getProperty()).append(":").append(error.getMessage()).append(" ");
        }
        return sb.toString();
    }

    public void addError(String propertyName, String message) {
        this.errors.add(new ErrorMessage(propertyName, message));
    }
}
